<template>
    <div class="app-center">
        <CustomHead />
        <div class="container">
            <div class="tabs">
                <span @click="onChangeTab(index)" :class="TabIndex == index ? 'active' : ''"
                    v-for="(tab, index) in Tabs" :key="index">{{ tab }}</span>
            </div>
            <div class="search">
                <el-input placeholder="搜索服务" v-model="input">
                    <template slot="append">
                        <el-button @click="getApp()" type="primary">搜索</el-button>
                    </template>
                </el-input>
            </div>
            <div class="content">
                <div class="left">
                    <el-tree class="filter-tree" ref="tree" :data="TabIndex?AppDataList:treeDeptList"
                             node-key="id" :props="defaultProps">
                        <span class="custom-tree-node" slot-scope="{ node, data }">
                            <span @click="getListAppById(data)">{{ node.label }}</span>
                            <!-- <span>({{ node.id }})</span> -->
                        </span>
                    </el-tree>
                </div>
                <div class="right">
                    <div class="app-list">
                        <div class="item" v-for="item in listApp"  :key="item.id">
                            <img :src="item.avatar" alt="" @click="toApp(item.url)">
                            <div class="right" @click="toApp(item.url)">
                                <span>{{ item.name }}</span>
                                <span>3人收藏</span>
                            </div>
                            <img class="collect-icon" :src="isCollected(item.id)?imgTwo:imgOne" alt=""  @click="onCollectionApp(item.id)">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import CustomHead from '../components/CustomHead.vue';
import { listApp, getAppById } from "@/api/app";
import { treeAllDept } from "@/api/department";
import { addCollection,getCollection,deleteCollection} from "@/api/user"
import imgOne from '@/assets/collect.png';
import imgTwo from '@/assets/collect-active.png';
export default {
    name: 'applicationCenter',
    components: {
        CustomHead,
    },
    data() {
        const data = [{
            id: 1,
            label: '全部',
            num: 72
        }, {
            id: 2,
            label: '学工服务',
            num: 2,
            children: [{
                id: 5,
                label: '信息管理',
                num: 36
            }, {
                id: 6,
                label: '行政管理',
                num: 10
            }]
        }, {
            id: 3,
            label: '网络办公流程',
            num: 8,
            children: [{
                id: 7,
                label: '后勤教辅服务',
                num: 5
            }, {
                id: 8,
                label: '教务教学管理',
                num: 3
            }]
        }, {
            id: 3,
            label: '网络办公流程',
            num: 8,
            children: [{
                id: 7,
                label: '后勤教辅服务',
                num: 5
            }, {
                id: 8,
                label: '教务教学管理',
                num: 3
            }]

        },
        {

            id: 3,
            label: '网络办公流程',
            num: 8,
            children: [{
                id: 7,
                label: '后勤教辅服务',
                num: 5
            }, {
                id: 8,
                label: '教务教学管理',
                num: 3
            }]
        },
        {
            id: 3,
            label: '网络办公流程',
            num: 8,
            children: [{
                id: 7,
                label: '后勤教辅服务',
                num: 5
            }, {
                id: 8,
                label: '教务教学管理',
                num: 3
            }]

        },
        {
            id: 1,
            label: '平台基础',
            num: 72
        },
        {
            id: 1,
            label: '教职工发展',
            num: 72
        },
        {
            id: 1,
            label: '全部',
            num: 72
        }
        ];
        return {
            AppCollection:[],
            imgOne,
            imgTwo,
            input: '',
            data: JSON.parse(JSON.stringify(data)),
            TabIndex: 0,
            Tabs: ["按部门查找", "按功能查找"],
            queryParams: {
                pageNum: 1,
                pageSize: 10
            },
            listApp: [],
            treeDeptList: [],//部门树
            defaultProps: {
                children: 'children',
                label: 'nodeName',
                id: 'sonNum'
            },
            quanbu:{
                    children: [],
                    deptOrAppId: 10025,
                    id: 1,
                    isLeave: 0,
                    nodeName: "全部",
                    nodeType: "department",
                    parentId: 1,
                    sonNum: 1,
            },
            AppDataList:[
                {
                    children: [],
                    deptOrAppId: 10025,
                    id: 2,
                    isLeave: 0,
                    nodeName: "服务类",
                    nodeType: "department",
                    parentId: 1,
                    sonNum: 1,
                },
                {
                    children: [
                        {
                            children: [],
                            deptOrAppId: 5,
                            id: 8,
                            isLeave: 1,
                            nodeName: "应用1",
                            nodeType: "department",
                            parentId: 1,
                            sonNum: 1,
                        },
                        {
                            children: [],
                            deptOrAppId: 5,
                            id: 3,
                            isLeave: 1,
                            nodeName: "应用2",
                            nodeType: "department",
                            parentId: 1,
                            sonNum: 1,
                        },
                        {
                            children: [],
                            deptOrAppId: 5,
                            id: 20,
                            isLeave: 1,
                            nodeName: "应用3",
                            nodeType: "department",
                            parentId: 1,
                            sonNum: 1,
                        },
                        {
                            children: [],
                            deptOrAppId: 5,
                            id: 16,
                            isLeave: 1,
                            nodeName: "应用4",
                            nodeType: "department",
                            parentId: 1,
                            sonNum: 1,
                        },
                        {
                            children: [],
                            deptOrAppId: 5,
                            id: 11,
                            isLeave: 1,
                            nodeName: "应用5",
                            nodeType: "department",
                            parentId: 1,
                            sonNum: 1,
                        },
                    ],
                    deptOrAppId: 10025,
                    id: 3,
                    isLeave: 0,
                    nodeName: "图书",
                    nodeType: "department",
                    parentId: 1,
                    sonNum: 1,
                },
                {
                    children: [],
                    deptOrAppId: 10025,
                    id: 4,
                    isLeave: 0,
                    nodeName: "服务类型查找应用开发中",
                    nodeType: "department",
                    parentId: 1,
                    sonNum: 1,
                },

            ]
        }
    },
    mounted() {
        this.getListApp();
        this.getTreeAllDept();
        this.getAppCollection();
        // 从缓存获取crp账号
        const crp = localStorage.getItem("CRP")
        
    },
    methods: {
        getAppCollection(){
            getCollection().then(res =>{
                this.AppCollection = res.data;
            }) 
        },
        getApp(){
            console.log("搜索app", this.input)
            listApp(
                {
                    keyword: this.input,
                    pageNum: 1,
                    pageSize: 10
                }
            ).then(res => {
                this.listApp = res.data.records;
            })
        },
        onChangeTab(index) {
            this.TabIndex = index;
        },
        getListApp() {
            listApp(this.queryParams).then(res => {
                console.log("获取app列表", res);
                this.listApp = res.data.records;
            })
        },
        toApp(url) {
            window.open(url, '_blank');
        },
        isCollected(id) {
           return this.AppCollection.some(item => item.id === id);
        },
        onCollectionApp(id){
            this.$message.closeAll(); // 关闭所有消息提示框
            const isCollected = this.isCollected(id);
            if (isCollected) {
               deleteCollection(id).then(res =>{
                this.$message.success('取消收藏成功');
                this.getAppCollection();
               })
            }else{
                addCollection(id).then(res =>{
                    this.$message.success('收藏成功');
                    this.getAppCollection(); 
                })
            }
        },
        getTreeAllDept() {
            treeAllDept().then(res => {
                this.treeDeptList = res[0].children;
                this.treeDeptList.unshift(this.quanbu);
                this.AppDataList.unshift(this.quanbu);
                console.log(this.treeDeptList);
            })
        },
        getListAppById(data) {
            this.listApp = [];
            if(data.id==1){
                console.log("查询全部app")
                listApp(this.queryParams).then(res => {
                    this.listApp = res.data.records;
                })
                return;
            }
            if(data.isLeave===1){
                console.log("根据下属id查询app",data.id)
                getAppById(data.id).then(res =>{
                    this.listApp.push(res.data)
                })
                return;
            }
            for(var i=0;i<data.children.length;i++){
                console.log("根据id查询app", data.children[i].id)
                getAppById(data.children[i].id).then(res =>{
                    this.listApp.push(res.data)
            })
            }  
        },
    }
}
</script>

<style scoped lang="scss">
::-webkit-scrollbar {
    display: none;
}

.app-center {
    background-color: #F1F3F5;
    padding-bottom: 50px;

    .container {
        margin: 0 80px;
        background-color: white;
        margin-top: 20px;

        @include respond-to('pad') {
            margin: 0 10px;
        }

        .tabs {
            flex: 1;
            padding: 25px 20px;

            span {
                margin-right: 20px;
                color: rgba($color: #333333, $alpha: 0.8);
                font-size: 18px;

                &.active {
                    color: #FA9E00;
                    padding-bottom: 2px;
                    font-weight: bold;
                    border-bottom: 2px solid #FA9E00;
                }
            }
        }

        .search {
            padding: 0 20px 20px;
            width: 50%;

            :deep .el-input-group__append {
                background-color: #0D67BB;
                color: white;
            }
        }

        .content {
            display: flex;
            margin-top: 20px;

            .left {
                display: flex;
                flex-direction: column;
                width: 200px;
                overflow: hidden;
                padding: 0 20px;
                border-right: 1px solid #cecece;
                overflow-y: auto; // 添加滚动条
                height: 550px; // 设置高度

                 /* 定制标准的滚动条样式 */
                scrollbar-width: thin; /* 滚动条宽度 */
                scrollbar-color: #888 #f1f1f1; /* 滚动条滑块颜色和轨道颜色 */
                
                .el-tree {
                    min-height: 800px;
                    margin-top: 15px;

                    :deep .el-tree-node__content {
                        margin-bottom: 4px;
                    }

                    :deep .el-tree-node {
                        padding: 10px 0;

                        &:hover {
                            background-color: #F5F7FA;
                        }
                    }

                    :deep .custom-tree-node {
                        white-space: nowrap;
                        text-overflow: ellipsis;
                        overflow: hidden;
                    }
                }
            }

            .right {
                flex: 1;
                padding: 0 15px 0;

                .app-list {
                    display: flex;
                    flex-wrap: wrap;
                    margin-top: 10px;

                    .item {
                        position: relative;
                        display: flex;
                        align-items: center;
                        margin-bottom: 15px;
                        width: 24%;
                        margin-right: 1.33%;
                        box-shadow: 0 2px 16px rgba(38, 66, 56, .2) !important;
                        padding: 0 20px;
                        box-sizing: border-box;
                        height: 100px;

                        @include respond-to('pad') {
                            width: 48%;
                            margin-right: 2%;

                            &:nth-child(2n) {
                                margin-right: 0;
                            }
                        }

                        &:nth-child(4n) {
                            margin-right: 0;
                        }

                        &>img:first-child {
                            width: 40px;
                            height: 40px;
                            margin-right: 5px;
                        }

                        .right {
                            display: flex;
                            flex-direction: column;
                            line-height: 30px;
                            white-space: nowrap;
                            text-overflow: ellipsis;
                            overflow: hidden;

                            span:first-child {
                                white-space: nowrap;
                                text-overflow: ellipsis;
                                overflow: hidden;
                            }

                            span:last-child {
                                color: rgba($color: #000000, $alpha: 0.5);
                            }
                        }

                        .collect-icon {
                            position: absolute;
                            right: 0px;
                            top: 0px;
                            height: 30px;
                        }
                    }
                }
            }
        }
    }
}
</style>